using Microsoft.AspNetCore.Mvc;
namespace BookShareWebApp
{

    public class DeleteController : Controller
    {

        private readonly BookContext _bookcontext;
        public DeleteController(BookContext bookcontext)
        {
            _bookcontext = bookcontext;
            
        }

        [HttpPost("/api/delete/{id}")]
        public JsonResult uploadinfo(int id){
            // remove database record
            Book book = _bookcontext.Book.Find(id);
            _bookcontext.Book.Remove(book);
            _bookcontext.SaveChanges();
            // remove file
            string filename = book.title;
            string fileDir = Path.Combine(Directory.GetCurrentDirectory(), "file");
            string path = fileDir + "/" + filename;
            FileInfo file = new FileInfo(path);
            if (file.Exists)
            {
                file.Delete();
            }

            return Json("Ok");
        }

    }
}